/*
2021-8-29
https://leetcode-cn.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/
*/
int a[15];
int n;
int t;
int res;
bool st[15];
int sum[15];

void dfs(int u,int s)
{
    if(s>=res) return ;
    if(u==n)
    {
        res=s;
        return ;
    }

    for(int i=0;i<s;i++)
    {
        if(sum[i]+a[u]<=t)
        {
            sum[i]+=a[u];
            dfs(u+1,s);
            sum[i]-=a[u];
        }
    }
    sum[s]=a[u];
    dfs(u+1,s+1);
    sum[s]=0;
}

class Solution {
public:
    int minSessions(vector<int>& tasks, int sessionTime) {
        n=tasks.size();
        for(int i=0;i<n;i++)
            a[i]=tasks[i];
        sort(a,a+n);
        reverse(a,a+n);
        res=1e9;
        t=sessionTime;

        memset(st,false,sizeof st);
        memset(sum,0,sizeof sum);

        dfs(0,0);
        return res;
    }

